package com.wgj.android.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.jiaai.xui.cache.XCache;
import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.XPreferencesUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Member;
import com.wgj.android.entity.Version;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.http.model.AccessToken;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DownloadAppUtils;
import com.wgj.android.utils.TextViweUtils;
import com.wgj.android.utils.fonat.IconFonat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/4.
 */

public class AppSettingActivity extends AppCompatActivity {
    @BindView(R.id.versionText)
    TextView versionText;

    @BindView(R.id.vT)
    TextView vT;

    @BindView(R.id.aT)
    TextView aT;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.setting_activity);
        ButterKnife.bind(this);
        versionText.setText(XAppUtils.getVersionName(this));
        setIcon();
    }

    private  void setIcon(){
        TextViweUtils.setDrawableLeft(vT, IconFonat.Icon.icon_banben, Color.parseColor("#3366CC"),8);
        TextViweUtils.setDrawableLeft(aT, IconFonat.Icon.icon_guanyu, Color.parseColor("#3366CC"),8);
    }

    @OnClick(R.id.versionBut)
    public void  onVersionClick(View view){
        XHttp.obtain().get(WGJAPI.CHECK_APP_VERSION, null, new HttpCallBack<Version>() {
            @Override
            public void onSuccess(final Version version) {
                int oldVersion= XAppUtils.getVersionCode(AppSettingActivity.this);
                if(version.getVersion()>oldVersion){
                    new XAlertMsgDialog(AppSettingActivity.this,version.getChangelog()).setTitle("发现新版本").setPositiveButton("下载").setListener(new XAlertMsgDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean b) {
                            if(b){
                                DownloadAppUtils.downloadForAutoInstall(AppSettingActivity.this,version.getInstallUrl(),"旺旺通app_v"+ version.getVersionShort(),"旺旺通发现新版本");
                            }

                        }
                    }).show();
                }else {
                    XToast.warning("当前是最新版本");
                }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });

    }

    @OnClick(R.id.aboutBut)
    public void onAboutButClick(View view){
        XToast.warning("即将上线");
    }

    @OnClick(R.id.loginOutBut)
    public void onLoginOutBut(View view){
        new XAlertMsgDialog(this,"是否退出当前登录?").setPositiveButton("退出").setNegativeButton("点错了").setListener(new XAlertMsgDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean b) {
                if(b){
                    CacheUtils.logout();
                    Intent intent=new Intent(getApplication(),LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }).show();

    }
}
